package com.google.android.gcm.demo.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

public class Tokens {
	private static final AtomicLong counter = new AtomicLong(System.currentTimeMillis());
	final Map<Long, List<String>> responses;
	final Map<Long, Date> generationTimes;
	
	public Tokens() {
		responses = new HashMap<Long, List<String>>();
		generationTimes = new HashMap<Long, Date>();
	}
	
	
	long nextToken() {
		long token = counter.getAndIncrement();
		responses.put(token, new ArrayList<String>());
		generationTimes.put(token, new Date());
		return token;
	}
	
	void addResponseReceived(long token, String client) {
		responses.get(token).add(client);
	}
	
	List<Long> getSentTokens() {
		List<Long> foo = new ArrayList<Long>(responses.keySet());
		Collections.sort(foo);
		return Collections.unmodifiableList(foo);
	}
	
	List<String> getRespondedClients(long token) {
		return Collections.unmodifiableList(responses.get(token));
	}
	
	Date getTimestamp(long token) {
		return generationTimes.get(token);
	}
	
}
